package com.wlt.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.mapper.ProductTypeMapper;
import com.wlt.pojo.ProductType;
import com.wlt.result.BusException;
import com.wlt.result.CodeEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@DubboService (timeout = 10000)
@Transactional
public class ProductTypeServiceImpl implements ProductTypeService
{
    @Autowired
    private ProductTypeMapper productTypeMapper;
    
    @Override
    public void add (ProductType productType)
    {
        // 根据父类型id查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        // 根据父类型的级别设置当前类型级别
        if (productTypeParent == null)              // 如果没有父类型，则为一级类型
        {
            productType.setLevel(1);
        }
        else if (productTypeParent.getLevel() < 3)  // 如果父类型级别小于3，则为父类型级别+1
        {
            productType.setLevel(productTypeParent.getLevel() + 1);
        }
        else if (productTypeParent.getLevel() == 3) // 如果父类型级别等于3，则为不能添加级别
        {
            throw new BusException(CodeEnum.INSERT_PRODUCT_TYPE_ERROR);
        }
        
        productTypeMapper.insert(productType);
    }
    
    @Override
    public void update (ProductType productType)
    {
        // 根据父类型id查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        // 根据父类型的级别设置当前类型级别
        if (productTypeParent == null)              // 如果没有父类型，则为一级类型
        {
            productType.setLevel(1);
        }
        else if (productTypeParent.getLevel() < 3)  // 如果父类型级别小于3，则为父类型级别+1
        {
            productType.setLevel(productTypeParent.getLevel() + 1);
        }
        else if (productTypeParent.getLevel() == 3) // 如果父类型级别等于3，则为不能添加级别
        {
            throw new BusException(CodeEnum.INSERT_PRODUCT_TYPE_ERROR);
        }
        
        productTypeMapper.updateById(productType);
    }
    
    @Override
    public void delete (Long id)
    {
        // 查询该类型是否有子类型
        QueryWrapper<ProductType> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("parentId", id);
        List<ProductType> productTypes = productTypeMapper.selectList(productQueryWrapper);
        // 如果有子类型，则删除失败
        if (!CollectionUtils.isEmpty(productTypes))
            throw new BusException(CodeEnum.DELETE_PRODUCT_TYPE_ERROR);
        
        productTypeMapper.deleteById(id);
    }
    
    @Override
    public ProductType findById (Long id)
    {
        return productTypeMapper.selectById(id);
    }
    
    @Override
    public Page<ProductType> search (ProductType productType, int page, int pageSize)
    {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if (productType != null)
        {
            // 当类型名不为空时
            if (StringUtils.hasText(productType.getName()))
                queryWrapper.like("name", productType.getName());
            
            // 上级类型id不为空时
            if (productType.getParentId() != null)
                queryWrapper.eq("parent_id", productType.getParentId());
        }
        
        return productTypeMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
    }
    
    @Override
    public List<ProductType> findProductType (ProductType productType)
    {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if (productType != null)
        {
            // 当类型名不为空时
            if (StringUtils.hasText(productType.getName()))
                queryWrapper.like("name", productType.getName());
            
            // 上级类型id不为空时
            if (productType.getParentId() != null)
                queryWrapper.eq("parent_id", productType.getParentId());
        }
        
        return productTypeMapper.selectList(queryWrapper);
    }
}
